<template>
    <div class="DeletePrompt">
       <div class="DeletePrompt-center">
           <div class="DeletePrompt-list" v-for="obj in dataSource">
               <div class="DeletePrompt-name"><span>{{i18n('id','主键')}}：</span><span>{{obj.value}}</span></div>
               <div class="DeletePrompt-name"><span>{{i18n('isDeleteYES','是否允许删除')}}：</span><span>{{obj.isDelete?i18n('YES','是'):i18n('NO','否')}}</span></div>
               <div class="DeletePrompt-name"><span>{{i18n('tips','提示')}}：</span><span>{{obj.prompt}}</span></div>
           </div>
       </div>
        <div class="DeletePrompt-bottom">
            <div class="DeletePrompt-ok" @click="handleSubmit">{{i18n('okBtn','确定')}}</div>
            <div class="DeletePrompt-cancellation" @click="cancel">{{i18n('cancelBtn','取消')}}</div>
        </div>
    </div>
</template>

<script>
import FormBoListApi from "@/api/FormBoList/FormBoListApi";

export default {
    name: "DeletePrompt",
    data(){
        return{
            resultData:[],
            valueTitle: "",
            dataSource: [],
            //允许删除的id
            deleteIds: [],
            //级联删除
            cascade: false,
            formAlias:''
        }
    },
      onLoad: function (option){
        this.resultData =option.resultData? JSON.parse(option.resultData):[];
        this.formAlias = option.formAlias?option.formAlias:'';
        this.init()
      },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formSolutionLang",key);
        },
        init(){
            if (this.resultData) {
                for (let i = 0; i < this.resultData.length; i++) {
                    var obj = {};
                    if (this.resultData[i].prompt) {
                        this.valueTitle = this.resultData[i].prompt;
                    }
                    if (this.resultData[i].value) {
                        obj["value"] = this.resultData[i].value;
                    } else {
                        obj["value"] = this.resultData[i].deleteId;
                    }
                    if (this.resultData[i].relationData) {
                        var prompt = "";
                        for (let j = 0; j < this.resultData[i].relationData.length; j++) {
                            var relationData = this.resultData[i].relationData[j];
                            if (!relationData.isDelete) {
                                obj["isDelete"] = false;
                                prompt = relationData.prompt || this.i18n('haveAssociated','关联了') + relationData.relatedEntName + this.i18n('noAllowedDel','不允许删除!');
                                break;
                            } else {
                                obj["isDelete"] = true;
                                if (relationData.cascade) {
                                    this.cascade = true;
                                    prompt = "【" +this.i18n('associationDel','关联删除')+ relationData.relatedEntName + "】";
                                }
                                this.deleteIds.push(this.resultData[i].deleteId);
                            }
                        }
                        obj["prompt"] = prompt;
                    }
                    this.dataSource.push(obj);
                }
            }
        },
        handleSubmit(){
            let self = this;
            let obj = {deleteIds: this.deleteIds.join(","), cascade: this.cascade}
            FormBoListApi.removeSolutionById({alias: this.formAlias, id: obj.deleteIds,cascade:obj.cascade}).then(res => {
              uni.showToast({
                title: res.message,
                duration: 1500,
                icon: 'none'
              });
                self.cancel();
                uni.$emit('onRefresh');
            });
        },
        cancel(){
          uni.navigateBack({
            delta: 1, // 返回上一页
            success: function () {
            }
          });
        }
    }
}
</script>

<style scoped>
.DeletePrompt{
    overflow: auto;
    height: 100%;
    display: flex;
    flex-direction: column;
}
.DeletePrompt-center{
    overflow: auto;
    flex: 1;
}
.DeletePrompt-list{
    overflow: hidden;
    background: #fff;
    margin: 0.5rem;
    border-radius: 0.3rem;
    box-shadow: 1px 1px 5px #dce1e6;
    padding: .5rem 1rem;
}
.DeletePrompt-bottom{
    height: 3rem;
    display: flex;
}
.DeletePrompt-ok{
    flex: 1;
    height: 3rem;
    line-height: 3rem;
    background: rgb(0, 122, 255);
    color: white;
    text-align: center;
}
.DeletePrompt-cancellation{
    flex: 1;
    height: 3rem;
    line-height: 3rem;
    text-align: center;
    background: red;
    color: white;
}
.DeletePrompt-name{
  height: 1.5rem;
  line-height: 1.5rem;
}
</style>
